/**
 * 
 */
package com.cqan.util.pool;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description 对象池，主要管理用户找回
 *  密码时给用户发送的验证码，可以设置缓存对象的有效期限
 * @author slaton.wu@gmail.com
 * @date 2012-7-20下午4:30:16
 *
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class ObjectCachePool<T> {

	private static final Logger logger = LoggerFactory.getLogger("ds.debug");
	/**
	 * 对象池，初始容量为 20
	 */
	private static final Map<String, CacheEntity> pool = new ConcurrentHashMap<String, CacheEntity>(20);
	
	/**
	 * 
	 */
	private static final ScheduledExecutorService scheduler = Executors
			.newScheduledThreadPool(1);

	private int delay = 10;
	
	private int validity=30;

	public ObjectCachePool() {
		logger.debug("初始化cache成功！");
		scheduler.scheduleAtFixedRate(new ClearTask(), 1, delay,TimeUnit.MINUTES);
	}
	
	public ObjectCachePool(int delay,int validity){
		this();
		this.delay = delay;
		this.validity = validity;
	}
	
	/**
	 * 添加对象到对象池
	 * @param key
	 * @param t
	 */
	public void put(String key,T t){
		CacheEntity<T> ce = new CacheEntity<T>(t);
		pool.put(key,ce);
		logger.debug("ObjectCachePool put key={} ce={}",new Object[]{key,ce});
	}
	
	/**
	 * 获取对象
	 * @param key
	 * @return
	 */
	public T get(String key){
		CacheEntity<T> ce = pool.get(key);
		logger.debug("ObjectCachePool get key={} ce={}",new Object[]{key,ce});
		return ce==null?null:ce.t;
	}
	
	/**
	 * 从对象池中删除对象
	 * @param key
	 */
	public void remove(String key){
		pool.remove(key);
		logger.debug("ObjectCachePool put remove={} ",key);
	}

	class ClearTask implements Runnable {
		
		public void run() {
			Set<Entry<String, CacheEntity>> e = pool.entrySet();
			for (Iterator iterator = e.iterator(); iterator.hasNext();) {
				
				Entry<String, CacheEntity> entry = (Entry<String, CacheEntity>) iterator.next();
				CacheEntity<T> ce = entry.getValue();
				if (ce.time<System.currentTimeMillis()-validity) {
					remove(entry.getKey());
					System.out.println("remove=="+entry.getKey());
				}
			}
		}
	}

	public int getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}

	public int getValidity() {
		return validity;
	}

	public void setValidity(int validity) {
		this.validity = validity;
	}
	
	

}

/**
 * 
 * @Description 缓存对象
 * @author slaton.wu@gmail.com
 * @date 2012-7-20下午5:20:24
 *
 * @param <T>
 */
class CacheEntity<T> {

	T t;
	long time;
	
	public CacheEntity(T t){
		this.t = t;
		this.time = System.currentTimeMillis();
	}

	@Override
	public String toString() {
		
		return ToStringBuilder.reflectionToString(this);
	}
	
	
}

